package com.kang.basicalgorithm.bubblesort;

import java.util.Arrays;

/**
 * @author xukang
 * @date 2021-10-22 15:23
 * 冒泡排序
 */
public class BubbleSortV2 {
    public static void main(String[] args) {
//        int[] array = {2,1,43,34,454,56,75,453,44,43,4,543,6,57,56,7,34,34,24,345,54,756,99};
        int[] array = {1, 2, 3, 4, 5, 6, 7};
//        int[] array = {5, 9, 7, 4, 1, 3, 2, 8};
        int n = array.length - 1;
        while (true){
            int turn = 1;
            int last = 0;
            for (int i = 0; i < array.length - 1; i++) {
                System.out.println("比较次数" + i);
                if (array[i] > array[i + 1]) {
                    swap(array, i, i + 1);
                    last = i;
                }
            }
            n = last;
            turn++;
            System.out.println("第"+turn+"轮"+Arrays.toString(array));
            if (n == 0) {
                break;
            }
        }
    }

    public static void swap(int[] array, int i, int j) {
        int t = array[i];
        array[i] = array[j];
        array[j] = t;

    }
}
